package com.example.makerspace.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.makerspace.domain.ExpertInfoDO;
import com.example.makerspace.dto.Pagination;
import com.example.makerspace.mapper.ExpertInfoMapper;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

@Component
public class ExpertInfoDAO extends  BaseDAO<ExpertInfoDO> {
    @Resource
    private ExpertInfoMapper expertInfoMapper;

    @PostConstruct
    public void init(){
        this.mapper = expertInfoMapper;
    }

    public IPage<ExpertInfoDO> selectByPage(Pagination pagination) {
        QueryWrapper<ExpertInfoDO> wrapper = new QueryWrapper<>();
        return  mapper.selectPage(new Page<ExpertInfoDO>(pagination.getPageNum(),pagination.getSize()),wrapper);
    }

    public IPage<ExpertInfoDO> selectByPageAndName(Pagination pagination, String name) {
        QueryWrapper<ExpertInfoDO> wrapper = new QueryWrapper<>();
       wrapper.like("name",name).or().like("phone",name).or().like("experience",name);;
        return  mapper.selectPage(new Page<ExpertInfoDO>(pagination.getPageNum(),pagination.getSize()),wrapper);
    }

    public IPage<ExpertInfoDO> selectByPageAndType(Pagination pagination,String type) {
        QueryWrapper<ExpertInfoDO> wrapper = new QueryWrapper<>();
        wrapper.eq("expert_type",type).eq("is_show",1);
        return  mapper.selectPage(new Page<ExpertInfoDO>(pagination.getPageNum(),pagination.getSize()),wrapper);
    }

    public  ExpertInfoDO selectByUserName(String username) {
        QueryWrapper<ExpertInfoDO> wrapper = new QueryWrapper<>();
        wrapper.eq("phone",username);
        return  mapper.selectOne(wrapper);
    }

    public IPage<ExpertInfoDO> selectByPageAndTypeName(Pagination pagination, String type, String name) {
        QueryWrapper<ExpertInfoDO> wrapper = new QueryWrapper<>();
        wrapper.eq("expert_type",type).eq("is_show",1).like("name",name);
        return  mapper.selectPage(new Page<ExpertInfoDO>(pagination.getPageNum(),pagination.getSize()),wrapper);
    }

    public IPage<ExpertInfoDO> selectByPageLikeName(Pagination pagination, String name) {
        QueryWrapper<ExpertInfoDO> wrapper = new QueryWrapper<>();
        wrapper.eq("is_show",1).like("name",name);
        return  mapper.selectPage(new Page<ExpertInfoDO>(pagination.getPageNum(),pagination.getSize()),wrapper);
    }

    public IPage<ExpertInfoDO> selectByPageIsShow(Pagination pagination) {
        QueryWrapper<ExpertInfoDO> wrapper = new QueryWrapper<>();
        wrapper.eq("is_show",1);
        return  mapper.selectPage(new Page<ExpertInfoDO>(pagination.getPageNum(),pagination.getSize()),wrapper);
    }
}
